/*
 * vec.cpp
 * Copyright (C) david 2010 <luodayuan2@gmail.com>
 * 
 */

#include "vec.h"


    template<class T>
void vec<T>::create()
{
    data = avail = limit = 0;
}

    template<class T>
void vec<T>::create(size_type n,const_reference val)
{
    data = alloc.allocate(n);
    limit = avail = data + n;
    unintilized_fill(data,limit,val);
}

    template <class T>
void vec<T>::create(const_iterator i,const_iterator j)
{
    data = alloc.allocate(j-i);
    limit = avail = unintilized_copy(i,j,data);
}

    template<class T>
void vec<T>::uncreate()
{
    if(data)
    {
        iterator it = avail;
        while(it != data)
            alloc.destory(--it);
        alloc.deallocate(data,limit - data);
    }
    data = limit = avail =0;
}

    template<class T>
void vec<T>::grow()
{
    size_type new_size = max(2*(limit -data),ptrdiff_t(1));
    iterator new_data = alloc.allocate(new_size);
    iterator new_avail = unintilized_copy(data,avail,new_data);
    uncreate();

    data = new_data;
    avail = new_avail;
    limit = new_data + new_size;
}

    template<class T>
void vec<T>::unchecked_append(const_reference val)
{
    alloc.construct(avail++,val);
}

    template <class T>
vec<T>& vec<T>::operator=(const vec& rhs)
{
    // check for self-assignment
    if (&rhs != this) {
        // free the array in the left-hand side
        uncreate();
        // copy elements from the right-hand to the left-hand side
        create(rhs.begin(), rhs.end());
    }
    return *this;
}











